package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.DB;
import util.Page;

import com.service.Impl.DoServiceImpl;
import com.vo.Do;

public class DoController extends HttpServlet {

	
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			doPost(request, response);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			
			String action=request.getParameter("action");
			if ("select".equalsIgnoreCase(action)) {
				Page pg=new Page();
				String pIndex=request.getParameter("pIndex");
				if (pIndex!=null) {
					int pI=Integer.parseInt(pIndex);
					pg.setpIndex(pI);
				}
				int zN=doServiceImpl.findzNum(db);
				pg.setzNum(zN);
				int zP=1;
				if (zN%pg.getPageNum()==0) {
					zP=zN/pg.getPageNum();
				} else {
					zP=zN/pg.getPageNum()+1;
				}
				pg.setzPage(zP);
				request.setAttribute("p", pg);
				
				List<Do> dos=doServiceImpl.findAllDo(db, pg);
				request.setAttribute("dos", dos);
				request.getRequestDispatcher("dos.jsp").forward(request, response);
				
			}else if ("delete".equalsIgnoreCase(action)) {
				
				String id=request.getParameter("d_id");
				doServiceImpl.deleteDoById(db, id);
				request.getRequestDispatcher("DoController?action=select").forward(request, response);
				
			}else if ("add".equalsIgnoreCase(action)) {
				
				request.getRequestDispatcher("add.jsp").forward(request, response);
				
			}else if ("addInfo".equalsIgnoreCase(action)) {
				String d_name=request.getParameter("d_name");
				String d_num=request.getParameter("d_num");
				String d_type=request.getParameter("d_type");
				String d_btw=request.getParameter("d_btw");
				
				Do d=new Do();
				d.setD_name(d_name);
				d.setD_num(d_num);
				d.setD_type(d_type);
				d.setD_btw(d_btw);
				
				doServiceImpl.addDo(db, d);
				request.getRequestDispatcher("DoController?action=select").forward(request, response);
							
			}else if ("update".equalsIgnoreCase(action)) {
				
				String id=request.getParameter("d_id");
				Do d=doServiceImpl.findDoById(db, id);
				request.setAttribute("d", d);
				request.getRequestDispatcher("update.jsp").forward(request, response);
				
			}else if ("updateInfo".equalsIgnoreCase(action)) {
				String d_id=request.getParameter("d_id");
				String d_name=request.getParameter("d_name");
				String d_num=request.getParameter("d_num");
				String d_type=request.getParameter("d_type");
				String d_btw=request.getParameter("d_btw");
				
				Do d=new Do();
				d.setD_id(d_id);
				d.setD_name(d_name);
				d.setD_num(d_num);
				d.setD_type(d_type);
				d.setD_btw(d_btw);
				
				doServiceImpl.updateDo(db, d);
				request.getRequestDispatcher("DoController?action=select").forward(request, response);
				
			}
	}

	
	public void init() throws ServletException {
		doServiceImpl=new DoServiceImpl();
		db=new DB();
	}
private DoServiceImpl doServiceImpl=null;
private DB db=null;
}
